package com.ygs.secs.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.corba.se.impl.ior.NewObjectKeyTemplateBase;
import com.ygs.secs.model.*;
import com.ygs.secs.service.*;
import com.ygs.secs.tools.CustomTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学生模块控制器
 *
 * @author Yunxi
 * @date 2020-11-20
 */
@RestController
@RequestMapping("/ygssecs")
@Configuration
@ImportResource(value = {"classpath:spring/spring-mybatis.xml"})
public class StudentController {
    @Autowired
    FamilyMemberService familyMemberService;
    @Autowired
    StudentService studentService;
    @Autowired
    IntentionService intentionService;
    @Autowired
    EventService eventService;
    @Autowired
    RecordService recordService;
    @Autowired
    AchievemenService achievemenService;
    @Autowired
    ResumeService resumeService;

    //上传图片真实路径
    @Value("${uppath}")
    String uploadPath;
    //映射路径
    @Value("${mappingpath}")
    String mappath;

    /**
     * 添加家庭成员
     *
     * @param famliyjosn 家庭成员json字符串
     * @return 通用json返回格式
     */
    @RequestMapping("/addfamilymember")
    public Map<String, Object> addfamilymember(String famliyjosn) {
        Map<String, Object> map = new HashMap<>();
        try {
            FamilyMember familyMember = JSON.parseObject(famliyjosn, FamilyMember.class);
            int i = familyMemberService.insertSelective(familyMember);
            if (i > 0) {
                map.put("code", 200);
                map.put("msg", "添加成功");
            } else {
                map.put("code", 201);
                map.put("msg", "添加失败");
            }
        } catch (DuplicateKeyException d) {
            d.printStackTrace();
            map.put("code", 500);
            map.put("msg", "不能重复添加哦");
            map.put("data", null);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "系统错误");
        }
        return map;
    }

    /**
     * 删除家庭成员
     *
     * @param familyid 家庭成员标识
     * @return 通用json返回结构
     */
    @RequestMapping("/delfamilymember")
    public Map<String, Object> delfamilymember(int familyid) {
        Map<String, Object> map = new HashMap<>();
        try {
            int i = familyMemberService.deleteByPrimaryKey(familyid);
            if (i > 0) {
                map.put("code", 200);
                map.put("msg", "删除成功");
            } else {
                map.put("code", 500);
                map.put("msg", "删除成功");
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "system error");
        }
        return map;
    }

    /**
     * 更新家庭成员信息
     *
     * @param familyjson 家庭成员json
     * @return 通用json返回格式
     */
    @RequestMapping("/upfamilymember")
    public Map<String, Object> upfamilymember(String familyjson) {
        Map<String, Object> map = new HashMap<>();
        try {
            FamilyMember familyMember = JSONObject.parseObject(familyjson, FamilyMember.class);
            int i = familyMemberService.updateByPrimaryKeySelective(familyMember);
            if (i > 0) {
                map.put("code", 200);
                map.put("msg", "修改成功");
            } else {
                map.put("code", 500);
                map.put("msg", "修改失败");
            }
        } catch (DuplicateKeyException d) {
            d.printStackTrace();
            map.put("code", 500);
            map.put("msg", "亲属姓名不能重复哦");
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "修改失败");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 学生登录
     *
     * @param stuid    学号
     * @param password 密码
     * @return 通用json返回格式
     */
    @RequestMapping("/studentlogin")
    public Map<String, Object> studentlogin(String stuid, String password) {
        Map<String, Object> map = new HashMap<>();
        try {
            Student student = studentService.selectByPrimaryKey(stuid);
            if (student != null) {
                if (student.getPassword().equals(password)) {
                    map.put("code", 200);
                    map.put("msg", "登录成功");
                    map.put("data", student);
                } else {
                    map.put("code", 201);
                    map.put("msg", "密码错误");
                    map.put("data", null);
                }
            } else {
                map.put("code", 500);
                map.put("msg", "账户不存在，请联系辅导员");
                map.put("data", null);
            }

        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "system error");
            map.put("data", null);
        }
        return map;
    }

    @RequestMapping("/stucheckpwd")
    public Map<String, Object> stucheckpwd(String stuid, String newpwd) {
        Map<String, Object> map = new HashMap<>();
        try {
            int checkpwdres = studentService.checkpwd(stuid, newpwd);
            if (checkpwdres > 0) {
                map.put("code", 200);
                map.put("msg", "密码修改成功");
            } else {
                map.put("code", 201);
                map.put("msg", "密码修改失败");
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "system error");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 获取学生详细信息
     *
     * @param stuid 学号
     * @return 通用json返回格式
     */
    @RequestMapping("/getstudetailinfo")
    public Map<String, Object> getstudetailinfo(String stuid) {
        Map<String, Object> map = new HashMap<>();
        try {
            Student student = studentService.getstudetailinfo(stuid);
            if (student != null) {
                map.put("code", 200);
                map.put("msg", "数据请求成功");
                map.put("data", student);
            } else {
                map.put("code", 201);
                map.put("msg", "数据请求失败");
                map.put("data", null);
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "system " +
                    "error");
            map.put("data", null);
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 获取家庭成员信息
     *
     * @param stuid 学生学号
     * @return 通用json返回格式
     */
    @RequestMapping("/getfamilymemberBystuid")
    public Map<String, Object> getfamilymemberBystuid(String stuid) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<FamilyMember> familyMemberList = familyMemberService.getfamilymemberBystuid(stuid);
            if (familyMemberList.size() > 0) {
                map.put("code", 200);
                map.put("msg", "数据查询成功");
                map.put("data", familyMemberList);
            } else {
                map.put("code", 201);
                map.put("msg", "你还未添加亲属数据哦");
                map.put("data", null);
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "system error");
            map.put("data", null);
        }
        return map;
    }

    /**
     * 查询就业意向
     *
     * @param stuid 学号
     * @return 通用json返回格式
     */
    @RequestMapping("/getintentionbystuid")
    public Map<String, Object> getintention(String stuid) {
        Map<String, Object> map = new HashMap<>();
        try {
            Intention intention = intentionService.selectByPrimaryKey(stuid);
            if (intention != null) {
                map.put("code", 200);
                map.put("msg", "数据查询成功");
                map.put("data", intention);
            } else {
                map.put("code", 201);
                map.put("msg", "数据查询成功");
                map.put("data", "你还未添加就业意向");
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "数据查询成功");
            map.put("data", "system error");
        }
        return map;
    }

    /**
     * 更新就业意向
     *
     * @param intentionjson 就业意向json字符串
     * @return 通用json返回格式
     */
    @RequestMapping("/updateIntention")
    public Map<String, Object> updateIntention(String intentionjson) {
        Map<String, Object> map = new HashMap<>();
        try {
            Intention intention = JSONObject.parseObject(intentionjson, Intention.class);
            Intention dintention = intentionService.selectByPrimaryKey(intention.getStudent().getStuid());
            if (dintention != null) {
                intention.setIntentid(dintention.getIntentid());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                intention.setUpdatetime(sdf.format(new Date()));
                int i = intentionService.updateByPrimaryKeySelective(intention);
                if (i > 0) {
                    map.put("code", 200);
                    map.put("msg", "就业意向更新成功");
                    map.put("data", intention);
                } else {
                    map.put("code", 201);
                    map.put("msg", "就业意向更新失败");
                }
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                intention.setUpdatetime(sdf.format(new Date()));
                int i = intentionService.insertSelective(intention);
                if (i > 0) {
                    map.put("code", 200);
                    map.put("msg", "就业意向添加成功");
                    map.put("data", intention);
                } else {
                    map.put("code", 201);
                    map.put("msg", "就业意向添加失败");
                }
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "system error");
        }
        return map;
    }

    /**
     * 添加校内经历
     *
     * @param stueventjson 校内时间json
     * @return 通用json格式
     */
    @RequestMapping("/addstuEvent")
    public Map<String, Object> addstuEvent(String stueventjson) {
        Map<String, Object> map = new HashMap<>();
        try {
            Event event = JSONObject.parseObject(stueventjson, Event.class);
            int i = eventService.insertSelective(event);
            if (i > 0) {
                map.put("code", 200);
                map.put("msg", "添加成功");
            } else {
                map.put("code", 500);
                map.put("msg", "添加失败");
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "添加失败");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 获取校内经历数据
     *
     * @param stuid 学号
     * @return 通用json返回格式
     */
    @RequestMapping("/getstuevent")
    public Map<String, Object> getstuevent(String stuid) {
        Map<String, Object> map = new HashMap<>();
        try {
            //获取学生校内经历
            List<Event> eventList = eventService.getstueventlist(stuid);
            if (eventList != null) {
                Map<String, Object> resmap = new HashMap<>();
                resmap.put("list", eventList);
                map.put("code", 200);
                map.put("msg", "数据请求成功");
                map.put("data", resmap);
            } else {
                map.put("code", 500);
                map.put("msg", "数据请求失败");
                map.put("data", null);
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "数据请求失败");
            map.put("data", null);
        }
        return map;
    }

    /**
     * 删除校内经历
     *
     * @param eventid 校内经历
     * @return 通用json返回格式
     */
    @RequestMapping("/delstueventbuid")
    public Map<String, Object> delstueventbuid(int eventid) {
        Map<String, Object> map = new HashMap<>();
        Event event = eventService.selectByPrimaryKey(eventid);
        String imaepath = event.getEventenclosure();
        boolean flag = CustomTools.deliamge(imaepath, mappath, uploadPath);
        if (flag) {
            int i = eventService.deleteByPrimaryKey(eventid);
            if (i > 0) {
                map.put("code", 200);
                map.put("msg", "删除成功");
            }
        } else {
            map.put("code", 500);
            map.put("msg", "删除失败");
        }
        return map;
    }

    /**
     * 查询应聘记录
     *
     * @param stuid 学号
     * @return 通用json
     */
    @RequestMapping("/getdeliveryRecord")
    public Map<String, Object> getdeliveryRecord(String stuid, @RequestParam(defaultValue = "1") int pageNum, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        try {
            //分页查询
            Page startPage = PageHelper.startPage(pageNum, pageSize);
            List<Record> recordList = recordService.getRecordlistBystuid(stuid);
            if (recordList != null) {
                map.put("code", 200);
                map.put("msg", "查询成功");
                map.put("data", recordList);
                map.put("count", startPage.getTotal());
            } else {
                map.put("code", 500);
                map.put("msg", "查询失败");
                map.put("data", null);
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "查询失败");
            map.put("data", null);
            e.printStackTrace();
        }
        return map;
    }


    /**
     * 添加专业成绩
     *
     * @param achievementjson 专业成绩json串
     * @return 通用json格式
     */
    @RequestMapping("/addAchievement")
    public Map<String, Object> addAchievement(String achievementjson) {
        Map<String, Object> map = new HashMap<>();
        try {
            if (achievementjson != null) {
                Achievemen achievemen = JSONObject.parseObject(achievementjson, Achievemen.class);
                int i = achievemenService.insertSelective(achievemen);
                if (i > 0) {
                    map.put("code", 200);
                    map.put("msg", "添加成绩成功");
                } else {
                    map.put("code", 500);
                    map.put("msg", "添加成绩失败");
                }
            } else {
                map.put("code", 500);
                map.put("msg", "添加成绩失败");
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "添加成绩失败");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 根据学期查询成绩
     *
     * @param semester 学期
     * @return 通用json格式
     */
    @RequestMapping("/getAchievementsbysemester")
    public Map<String, Object> getAchievementsbysemester(String semester, @RequestParam(required = true) String stuid) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Achievemen> achievemenList = achievemenService
                    .getAchievementsbysemester(semester, stuid);
            if (achievemenList != null) {
                map.put("code", 200);
                map.put("msg", "数据查询成功");
                map.put("data", achievemenList);
            } else {
                map.put("code", 500);
                map.put("msg", "数据查询失败");
                map.put("data", null);
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "数据查询失败");
            map.put("data", null);
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 删除成绩
     *
     * @param achieveid 成绩id
     * @return 通用json格式
     */
    @RequestMapping("/delachievementbyid")
    public Map<String, Object> delachievementbyid(int achieveid) {
        Map<String, Object> map = new HashMap<>();
        try {
            int i = achievemenService.deleteByPrimaryKey(achieveid);
            if (i > 0) {
                map.put("code", 200);
                map.put("msg", "删除成功");
            } else {
                map.put("code", 500);
                map.put("msg", "删除失败");
            }
        } catch (Exception e) {
            map.put("code", 200);
            map.put("msg", "删除失败");
        }
        return map;
    }

    /**
     * 更新成绩
     *
     * @param achievejson 成绩json数据
     * @return 通用json'格式
     */
    @RequestMapping("/upAchievement")
    public Map<String, Object> upAchievement(String achievejson) {
        Map<String, Object> map = new HashMap<>();
        try {
            Achievemen achievemen = JSONObject.parseObject(achievejson, Achievemen.class);
            int i = achievemenService.updateByPrimaryKeySelective(achievemen);
            if (i > 0) {
                map.put("code", 200);
                map.put("msg", "成绩更新成功");
            } else {
                map.put("code", 500);
                map.put("msg", "成绩更新失败");
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "成绩更新失败");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 添加简历
     *
     * @param resumejson 简历json
     * @return 通用json数据格式
     */
    @RequestMapping("/addresume")
    public Map<String, Object> addresume(String resumejson) {
        Map<String, Object> map = new HashMap<>();
        try {
            Resume resume = JSONObject.parseObject(resumejson, Resume.class);
            int count = resumeService.getresumecount(resume.getStudent().getStuid());
            if (count <= 5) {
                int i = resumeService.insertSelective(resume);
                if (i > 0) {
                    map.put("code", 200);
                    map.put("msg", "简历上传成功");
                } else {
                    map.put("code", 500);
                    map.put("msg", "简历上传失败");
                }
            } else {
                map.put("code", 500);
                map.put("msg", "最多可添加5份简历");
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "简历上传失败");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 获取简历列表
     *
     * @param stuid 学号
     * @return 通用json
     */
    @RequestMapping("/getresumelistbystuid")
    public Map<String, Object> getresumelistbystuid(String stuid) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Resume> resumeList = resumeService.getresumelist(stuid);
            if (resumeList != null) {
                map.put("code", 200);
                map.put("msg", "数据查询成功");
                map.put("data", resumeList);
            } else {
                map.put("code", 500);
                map.put("msg", "数据查询失败");
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "数据查询失败");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 删除简历
     *
     * @param resumeid 简历id
     * @return 通用json
     */
    @RequestMapping("/delresume")
    public Map<String, Object> delresume(int resumeid) {
        Map<String, Object> map = new HashMap<>();
        try {
            Resume resume = resumeService.selectByPrimaryKey(resumeid);
            String resumepath = resume.getResumepath();
            CustomTools.deliamge(resumepath, mappath, uploadPath);
            int i = resumeService.deleteByPrimaryKey(resumeid);
            if (i > 0) {
                map.put("code", 200);
                map.put("msg", "删除成功");
            } else {
                map.put("code", 500);
                map.put("msg", "删除失败");
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "删除失败");
        }
        return map;
    }

    /**
     * 根据id获取学生信息
     *
     * @param stuid 学生id
     * @return 通用json格式
     */
    @RequestMapping("/getstudentbyid")
    public Map<String, Object> getstudentbyid(String stuid) {
        Map<String, Object> map = new HashMap<>();
        try {
            Student student = studentService.selectByPrimaryKey(stuid);
            if (student != null) {
                map.put("code", 200);
                map.put("msg", "数据请求成功");
                map.put("data", student);
            } else {
                map.put("code", 500);
                map.put("msg", "数据请求失败");
                map.put("data", null);
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "数据请求失败");
            map.put("data", null);
        }
        return map;
    }

    /**
     * 修改学生信息
     *
     * @param stujson 学生实体json数据
     * @return 通用json格式
     */
    @RequestMapping("/upstudentinfo")
    public Map<String, Object> upstudentinfo(String stujson) {
        Map<String, Object> map = new HashMap<>();
        try {
            Student student = JSONObject.parseObject(stujson, Student.class);
            int i = studentService.updateByPrimaryKeySelective(student);
            if (i > 0) {
                map.put("code", 200);
                map.put("msg", "信息修改成功");
            } else {
                map.put("code", 500);
                map.put("msg", "信息修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "信息修改失败");
        }
        return map;
    }
}
